#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int displayClock(int hours, int minutes);

/* This is a test program for displayClock(). It is provided for your
 * information and to understand how the displayClock() function is
 * invoked.
 */
int main()
{
    int h, m;
    int display_error, end_of_input;

    /* Note: You should __never__ call scanf in real code.
     * This is quick for testing. */

    printf("\nEnter <hours> <minutes>: ");
    scanf("%d%d", &h, &m);

    display_error = displayClock(h, m);
    if (display_error)
        printf("Incorrect data received for hours and minutes\n");

    return 0;
}

int displayClock(int h, int m)
{
    char clock[9][20] = { /* The array to hold the hard-coded clock */
        "        12         ",
        "   11        01    ",
        " 10            02  ",
        "                   ",
        "09      .       03 ",
        "                   ",
        " 08            04  ",
        "   07        05    ",
        "        06         "
    };

    /* Row and column numbers in clock array for hour positions. For
     * example, the "06" in the clock display is stored at position
     * [8, 8] in the clock matrix (see above) */
    const int hr[13] = {0, 1, 2, 4, 6, 7, 8, 7, 6, 4, 2, 1, 0};
    const int hc[13] = {0, 13, 15, 16, 15, 13, 8, 3, 1, 0, 1, 3, 8};

    /* Hour and minute codes */
    const char hs = 'H', ms = 'm';

    int i, j; /* Declare other variables as needed */

    /* The next 5 comments require code to be developed by you! */

    /* Ensure that h and m are in range and, if not, return
     * appropriate error value. Otherwise adjust h if it is 0 or it
     * exceeds 12
     */
         
    if ( h>=0 && h<24 && m>=0 && m<60 )
    {
      if ( h> 12 )
        h -= 12; 
      if ( h == 0 )
        h = 12;
    }
    else
      return -1;

    /* Now update the clock array accordingly, with the hour symbol */
    clock[ hr[h] ][ hc[h] ] = clock[ hr[h] ][ hc[h]+1 ] = hs; 

    /* Round m to the nearest hour position */
    if( m%5 != 0 )
    {
       m = (m%5 > 2)?m/5 + 1:m/5; 
    } 
    else
    {
      m /= 5; 
    }

    /* Update clock with minute symbol, taking into account a "clash"
     * with the hour symbol
     */
    if ( m == 0 )
      m = 12;
    
    if( m == h )
    {
      clock[ hr[m] ][ hc[m] ] = ms; 
    }
    else
    {
      clock[ hr[m] ][ hc[m] ] = clock[ hr[m] ][ hc[m]+1 ] = ms;  
    } 

 
    /* Display the clock */
    for (i = 0; i < 9; i++)
    {
        for (j = 0; j < 19; j++)
    	    printf("%c", clock[i][j]);
        printf("\n");
    }
    printf("\n");

    return 0;
}

